/** @file
 *  Library to manage Newport ESP301 Motion controller.
 *  @example esp301-test.cpp
 */
#ifndef __ESP301_CONTROL__
#define __ESP301_CONTROL__

#include <string>
#include <serialib.h>

/** Buffer size of the Serial interface */
const int RS232_BUFFER_MAX = 128;

/** Default software lower limit of the actuator */
const double ESP301_LOWER_LIMIT = 0.;

/** Default software upper limit of the actuator */
const double ESP301_UPPER_LIMIT = 7.;

/** This class manages the Newport ESP301 motion controller */
class esp301_control:private serialib
{

  /** Return value from member functions of class serialib */
  int ret;

  /** buffer to temporarily storing bytes from serial interface */
  char buffer[RS232_BUFFER_MAX];

  /** Go to home position and reset displacement. */
  void goHome ();

  /** Set software limits */
  void setLimit ();

  /** Check for errors from the motion controller */
  void chkError ();

  /** Set tolerance */
  void setTolerance (float tol);

public:

  /** Blind estimation of current position */
  double theoretical_displacement;

  /** Connect to motion controller.
   *   @return 1 if no error; 0 otherwise
   */
  bool connect ();

  /** Constructor */
    esp301_control ();

  /** Destructor */
   ~esp301_control ();

  /** Get current position.
   *  @return current actuator position in millimeter
   */
  float getPosition ();

  /** Move to absolute position
   *  @param x absolute position
   *  @return 1 if no error; 0 otherwise
   */
  bool moveTo (double x);

  /** Move to relative position
   *  @param dx relative position
   *  @return 1 if no error; 0 otherwise
   */
  bool moveBy (double dx);

  /** Go home and set software limits */
  void preset ();
};

#endif
